/*
 * Copyright (c) 2006-2023, xxxx Development Team
 *
 *
 * Change Logs:
 * Date           Author       Version      Notes
 * 2022-11-19     TSL          v0.1         first version
 */
/* 头文件 ---------------------------------------------------------------------*/
#include "main.h"
#include "module_beep.h"

/* 宏定义 ---------------------------------------------------------------------*/
#define BEEP_DEV_INIT_AND_REGISTER(_ID_) do {        \
    fsl_ioctl_init_dev(&dev_beep[_ID_]);             \
    dev_beep[_ID_].on = beep##_ID_##_on;              \
    dev_beep[_ID_].off = beep##_ID_##_off;            \
    fsl_ioctl_register_dev(&dev_beep[_ID_]);         \
} while(0)

/* 类型定义 -------------------------------------------------------------------*/

/* 函数声明 -------------------------------------------------------------------*/
static void beep_init(void);
static void beep_setup(uint8_t index, uint16_t on_time_ms,uint16_t off_time_ms,uint8_t repeat_num);
static uint8_t beep_is_busy(uint8_t index);

static void beep0_on(void);
static void beep0_off(void);
static void beep1_on(void);
static void beep1_off(void);

/* 变量声明 -------------------------------------------------------------------*/
static ioctl_dev_t dev_beep[BEEP_NUM_MAX];

const struct beep_ops api_beep = {
    .init = beep_init,
    .setup = beep_setup,
    .is_busy = beep_is_busy,
};

/*
 * 函数说明
 */
static void beep_init(void)
{
    // BEEP硬件初始化
    // ... ...

    // BEEP实例初始化
    BEEP_DEV_INIT_AND_REGISTER(0);
    BEEP_DEV_INIT_AND_REGISTER(1);
}

/*
 * 函数说明
 */
static void beep_setup(uint8_t index, uint16_t on_time_ms,uint16_t off_time_ms,uint8_t repeat_num)
{
    // 关键参数合法性检查
    if (index >= BEEP_NUM_MAX) return;

    fsl_ioctl_setup(&dev_beep[index], on_time_ms, off_time_ms, repeat_num);
}

/*
 * 函数说明
 */
static uint8_t beep_is_busy(uint8_t index)
{
    // 关键参数合法性检查
    if (index >= BEEP_NUM_MAX) return 0;

    return fsl_ioctl_is_busy(&dev_beep[index]);
}

/*
 * 函数说明
 */
static void beep0_on(void)
{
    dbg_print_string("\nBEEP0 ON!!!\n");
}
static void beep0_off(void)
{
    dbg_print_string("\nBEEP0 OFF!!!\n");
}
static void beep1_on(void)
{
    dbg_print_string("\nBEEP1 ON!!!\n");
}
static void beep1_off(void)
{
    dbg_print_string("\nBEEP1 OFF!!!\n");
}
